<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>采购入库</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
	<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script type="text/javascript">
var basic_Url="${ctx}/yd/logic/profitandloss";
var grid;
var url;
$(function(){
	
	grid = $("#table1").ligerGrid({
        columns: [
					{ display: 'id', name: 'id', hide:1 },
					{ display: 'code', name: 'code', hide:1 },
                  { display: '品名', name: 'name', width: 180 },
                  { display: '品牌', name: 'brand', width: 180},
                  { display: '规格', name: 'spec', width: 120 },
                  { display: '单位', name: 'unit', width: 120},
                  { display: '入库数量', name: 'quantity', width: 120},
                  { display: '损失数量', name: 'healthycount', width: 120,editor: { type: 'float'},render:function(item){
					  grid.updateCell('badcount', ((item.healthycount/item.quantity)*100).toFixed(2), item); 
                   	  return item.healthycount;
                  }},
                  { display: '损益率', name: 'badcount', width: 120}
                  ],
                  enabledEdit: true,
                  usePager: true,pageSize:1000,pageSizeOptions:[1000],
                  width: "100%", height:250, checkbox: true,
                  toolbar: { items: [
                                     { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                     ]
                                     }
                  });
	$("#pageloading").hide();
	
	$("#inputForm").validate({			   
	onsubmit: true,
	 /*执行ajaxsubmit  */ 
    submitHandler: function(editform) {
        try{			    		
            url =  basic_Url+"/save.do";
            var api = frameElement.api;
        	var W = api.opener;
            getReturnMessage(url,"inputForm",W.searchAP);
        }catch(e){alert(e)}
    }
	});

	//$("#storage").change(function(){ 
	//	  $("#whcode").val($("#storage").val());
	//	  $("#whname").val($("#storage").find("option:selected").text());
	//}); 
	
	$("#djlx1").change(function(){ 
		  $("#djlx").val($("#djlx1").val());
		  if($("#djlx1").val()=="1"){
			  $.ajax({
			      url: "${ctx}/yd/logic/profitandloss/powarelist.do",
			      global: false,
			      type: "POST",
			      dataType: "json",
			      data: ({
			    	  type : $("#djlx1").val()
			      }),		     
			      success: function(msg){
			    	  $("#oricode1").html("<option></option>");
			    	  if (msg != null && msg.length >= 1){
			    		  for (var i = 0; i <msg.length; i++){
			    				  $("#oricode1").html($("#oricode1").html() + "<option id='"+msg[i].powarecode+"' value='"+msg[i].id+"'>"+msg[i].powarecode+"</option>");
			    		  }
			    	  }
			    	 
			   }
			});
		  }
		  if($("#djlx1").val()=="2"){
			  $.ajax({
			      url: "${ctx}/yd/logic/profitandloss/solist.do",
			      global: false,
			      type: "POST",
			      dataType: "json",
			      data: ({
			    	  type : $("#djlx1").val()
			      }),		     
			      success: function(msg){
			    	  $("#oricode1").html("<option></option>");
			    	  if (msg != null && msg.length >= 1){
			    		  for (var i = 0; i <msg.length; i++){
			    				  $("#oricode1").html($("#oricode1").html() + "<option id='"+msg[i].sowarecode+"' value='"+msg[i].id+"'>"+msg[i].sowarecode+"</option>");
			    		  }
			    	  }
			    	 
			   }
			});
		  }
	}); 
	
	
	$("#bslx1").change(function(){ 
		  $("#bslx").val($("#bslx1").val());
	}); 
	
	//$("#kdgs").change(function(){ 
	//	$("#creategsname").val($("#kdgs").find("option:selected").text());
	//	$("#creategscode").val($("#kdgs").val());
	//}); 
	
	$("#oricode1").change(function(){ 
		 $("#oricode").val($("#oricode1").find("option:selected").text());
		 if($("#djlx1").val()=="1"){
		<c:forEach items='${powarelist}' var='state'>
        	if($("#oricode1").find("option:selected").text()=='${state.powarecode}'){
        		$("#aktscode").val('${state.pocode}');
        		var rkrq=new Date('${state.dhrq}');
        		$("#rkrq").val(formatDate(rkrq,"yyyy-MM-dd"));
        		//$("#"+'${state.creategscode}').attr("selected","selected");
        		//$("#"+'${state.whcode}').attr("selected","selected");
        		$("#whcode").val('${state.whcode}');
        		$("#whname").val('${state.whname}');
        		$("#creategsname").val('${state.creategsname}');
        		$("#creategscode").val('${state.creategscode}');
        	}
       	</c:forEach>
		multipleSearch("#table1",true,"",basic_Url+"/poitemlist.do?poid="+$("#oricode1").val());
		 }
		 if($("#djlx1").val()=="2"){
				<c:forEach items='${splist}' var='state'>
		        	if($("#oricode1").find("option:selected").text()=='${state.sowarecode}'){
		        		$("#aktscode").val('${state.pocode}');
		        		var aptime=new Date('${state.aptime}');
		        		$("#rkrq").val(formatDate(aptime,"yyyy-MM-dd"));
		        		//$("#"+'${state.creategscode}').attr("selected","selected");
		        		//$("#"+'${state.whcode}').attr("selected","selected");
		        		$("#whcode").val('${state.whcode}');
		        		$("#whname").val('${state.whname}');
		        		$("#creategsname").val('${state.creategsname}');
		        		$("#creategscode").val('${state.creategscode}');
		        	}
		       	</c:forEach>
				multipleSearch("#table1",true,"",basic_Url+"/soitemlist.do?poid="+$("#oricode1").val());
		 }
	}); 
	
	if ('${flag}' == 'ADD'){
		 $("#tr1").hide();
		 $("#apply4").hide();
	}
	if ('${flag}' == 'EDIT'){
		 $("#apply13").hide();
		 $("#li1").hide();
	}
	
	<c:forEach items='${wareitem}' var='item'>
    	grid.addRow({
    		id: "${item.id}",
    		code: "${item.code}",
    		name: "${item.name}",
    		brand: "${item.brand}",
    		spec: "${item.spec}",
    		unit : "${item.unit}",
    		quantity : "${item.quantity}",
    		badcount : "${item.badcount}",
    		healthycount : "${item.healthycount}"
        });
    </c:forEach>
})

	function addEditRow(){
		var manager = $("#table2").ligerGetGridManager(); 
		manager.addEditRow(); 
	}

	function gridfreigdeleteRow()
	{ 
		gridfreig.deleteSelectedRow();
	}

function submitForm(param){
    //设置ajax全局同步
    
	//var data = JSON.stringify(grid.getData());
	var data = grid.getData();
	var dl=grid.getDeleted();
	if(data.length==0){
		$.dialog.alert("请选择申请物料");
   	 	return false;
	}
	var ba=true;
	if(data !=null){
		var i=0;
		$(data).each(function (){
			i++;
				if (this.healthycount >= 0) {
					if (this.incount == null)
						this.incount = 0;
					if (parseInt(this.quantity) < parseInt(this.healthycount)){
						$.dialog.alert("您第" + i + "行的报损数量大于入库数量填写");
						ba = false;
						return false;
					}
				}else{
					$.dialog.alert("您第" + i + "行的报损数量不能小于0");
					ba = false;
					return false;
				}
				if (this.healthycount == ""||this.healthycount==null) {
					ba = false;
					$.dialog.alert("您第" + i + "行的报损数量没有填写");
					return false;
				}
			});
		}
		jQuery.ajaxSettings.async = false;
		if (ba) {
			$("#type").val(param);
			$("#itemdata").val(JSON.stringify(data));
			$("#itemdelete").val(JSON.stringify(dl));
			$("#submitFlag").val(param);
			$("#inputForm").submit();
		}
	}

	function deleteRow() {
		grid.deleteSelectedRow();
	}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${poware.id}"/>
<input type="hidden" id="itemdata" name="itemdata" value=""/>
<input type="hidden" id="itemdelete" name="itemdelete" value=""/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
         <table class="TableBlock" align="center">
                   <tr id="tr1">
                    <td nowrap>损益单据号:</td>
   		                 <td colspan="6">
   		                <input type="text" id="procode" name="procode" disabled="disabled" class="required" value="${poware.procode}"  size="50"/>
   		                 </td>
   		           </tr>
   		           <tr>
   		          		<td nowrap>单据类型:</td>
                    	<td colspan="6">
                    	<select id="djlx1" name="djlx1" class="required">
   		                 	<option></option>
   		                 	<option id="1" value="1">入库</option>
   		                 	<option id="2" value="2">出库</option>
   		                 </select>
   		                 <script type="text/javascript">
   		                		 $("#"+'${poware.djlx}').attr("selected","selected");
   		                 </script>
   		                 <input type="hidden" id="djlx" name="djlx" value="${poware.djlx}"/>
                    	</td>
   		               
   		           </tr>
   		            <tr>
   		                <td nowrap>出入库单号:</td>
   		                <td id="apply13">
   		                 <select id="oricode1" name="oricode1" class="required">
   		                 </select>
   		                 	<input type="hidden" id="oricode" name="oricode" value="${poware.oricode}"/>
   		                 	<script type="text/javascript">
   		                		 $("#"+'${poware.oricode}').attr("selected","selected");
   		                 	</script>
   		                </td>
   		                <td id="apply4">
   		                 	<input type="text" id="oricode"  disabled="disabled" name="oricode" value="${poware.oricode}" size="40"/>
   		                </td>
   		                <td nowrap>采购编号:</td>
   		                <td nowrap>
   		                 	<input type="text" id="aktscode" class="required" readonly="readonly" name="aktscode" value="${poware.aktscode}"/>
   		                </td>
   		           </tr>
   		           <tr>
   		                <td nowrap>损益仓库:</td>
                    	<td><!-- <select id="storage" name="storage" class="required">
   		                 	<option></option>
   		                 	<c:forEach items='${wh}' var='state'>
   		                 	<option id="${ state.code}" value='${state.code}'>${state.name}</option>
   		                 	</c:forEach>
   		                 	</select>
   		                 	<input type="hidden" id="whcode" name="whcode" value="${poware.whcode}"/>
   		                 	<input type="hidden" id="whname" name="whname" value="${poware.whname}"/>
   		                 	<script type="text/javascript">
   		                		 $("#"+'${poware.whcode}').attr("selected","selected");
   		                 	</script>
   		                 	 -->
   		                 	 <input type="hidden" id="whcode" name="whcode" value="${poware.whcode}"/>
   		                 	<input type="text" readonly="readonly" style="width: 200px;" id="whname" name="whname" value="${poware.whname}"/>
   		                 	</td>
   		                 <td nowrap>报损类型:</td>
   		                 <td>
   		                	<select id="bslx1" name="bslx1" style="width: 200px;" class="required">
   		                 	<option></option>
   		                 	<option id="入库短少" value="入库短少">入库短少</option>
   		                 	<option id="拆柜短少" value="拆柜短少">拆柜短少</option>
   		                 	<option id="商检取样" value="商检取样">商检取样</option>
   		                 	<option id="报废损失" value="报废损失">报废损失</option>
   		                 	<option id="运输损耗" value="运输损耗">运输损耗</option>
   		                 	<option id="其他" value="其他">其他</option>
   		                 </select>
   		                 <script type="text/javascript">
   		                		 $("#"+'${poware.bslx}').attr("selected","selected");
   		                 	</script>
   		                 <input type="hidden" id="bslx" name="bslx" value="${poware.bslx}"/>
   		                 </td>
   		           </tr>
   		           
   		            <tr>
   		           		<td nowrap>入库日期:</td>
                    	<td>
                    	<input type="text" id="rkrq" name="rkrq" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="required" value="<fmt:formatDate value="${poware.rkrq}" pattern="yyyy-MM-dd" type="both" />"  readonly="readonly"/>
                    	</td>
                    	<td nowrap>操作人:</td>
   		                 <td>
   		                	<input type="text" id="aegnt" name="aegnt" value="${poware.aegnt}"/>
   		                 </td>
   		           </tr>
   		            <tr>
   		            <td>开单公司:</td>
   		                 <td><!-- <select id="kdgs" name="kdgs" class="required" style="width: 100px;">
   		                 	<option id="" value=''></option>
   		                 	<c:forEach items='${bb}' var='state'>
   		                 	<option id="${state.code}" value='${state.code}'>${state.name}</option>
   		                 	</c:forEach>
   		                 	</select>
   		                 	<input type="hidden" id="creategscode" name="creategscode" value="${poware.creategscode}"/>
   		                 	<input type="hidden" id="creategsname" name="creategsname" value="${poware.creategsname}"/>
   		                 	<script type="text/javascript">
   		                		 $("#"+'${poware.creategscode}').attr("selected","selected");
   		                 	</script> -->
   		                 	<input type="hidden" id="creategscode" name="creategscode" value="${poware.creategscode}"/>
   		                 	<input type="text" style="width: 300px;" id="creategsname" readonly="readonly" name="creategsname" value="${poware.creategsname}"/>
   		                 	</td>
   		                 	  
   		           	
   		           </tr>
   		           <tr>
   		           	<td>备注:</td>
   		           	<td colspan="3"><input style="width: 98%;" id="remark" name="remark" value="${poware.remark}" /></td>
   		           </tr>
                </table>
                <div id="table1"> </div> 
         </div>
         <div class="formBar">
	        <ul>
		       <li id="li1"><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm(1)">提交</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
